package view;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.org.apache.bcel.internal.generic.NEW;

import database.DbFile;
import database.DbFolder;
import database.User;


/**
 * Servlet implementation class ServerServlet
 */

public class SharedFolderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	//GetUserCommand getUserCommand;
	AddSharedFolderCommand addSharedFolderCommand;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SharedFolderServlet() {
        super();
        // TODO Auto-generated constructor stub
        //getUserCommand = new GetUserCommand();
        addSharedFolderCommand = new AddSharedFolderCommand();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setStatus(HttpServletResponse.SC_OK);
                
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String userID = (String) request.getSession().getAttribute("userID");
        String password = (String) request.getSession().getAttribute("password");
        String sharedUserID = request.getParameter("userSharedID").toString(); 
        String folderName = request.getRequestURI().substring(request.getRequestURI().lastIndexOf("/")+1);
        
		//User user = getUserCommand.action(userID);
        
        out.println("<html>");
        out.println("<head>");
        out.println("<title> clever dropbox =) </title>");
        out.println("</head>");
        out.println("<body>");
        
        Integer result = addSharedFolderCommand.action(userID, folderName, sharedUserID);
        
        if ((result!=null) && (!result.equals(0)))
        {
        	out.println("you shared folder "+ folderName + " successfully with "+ sharedUserID + "<BR>");
        }
        else
        {
        	out.println("the folder sharing failed <BR>");
        	
        }
        
        
        out.println("<a href='/UserDropboxServlet/folder/" + folderName +"'> back </a>");
        
        
        out.println("</body>");
        out.println("</html>");
        
        //userDropboxHome(userID,password,out);

        out.close();

	}


}
